package com.example.agriculture.controller;

import com.example.agriculture.entity.AdminInfor;
import com.example.agriculture.entity.Producer;
import com.example.agriculture.service.AdminInforService;
import com.example.agriculture.service.ProducerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BmsLoginController {

    @Resource
    private AdminInforService adminInforService;

    @Resource
    private ProducerService producerService;

    //登录主页面
    @GetMapping({"/admin"})
    public String AdminLogin(){
        return "BMS/login";
    }

    //接收表单传过来的数据
    @PostMapping("/main")
    //这两个的名字要和html中的表单一样，用来接收表单的数据
    public ModelAndView GetAdminLogin(String number, String password, String type , HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("BMS/login");
        //进行登录的判断
        if (type==null){
            modelAndView.setViewName("BMS/login");
            System.out.println("请选择登录后台管理的类型");
            return modelAndView;
        }else if(type.equals("管理员")){
            int count = adminInforService.login(number);
            System.out.println(count);
            if(count==1){
                AdminInfor adminInfor = adminInforService.adminlogin(number, password);
                modelAndView.setViewName("redirect:/AIndex");
                session.setAttribute("admininfor",adminInfor);
                return modelAndView;
            }else if (count==0){
                modelAndView.setViewName("BMS/login");
                System.out.println("账号密码没有输入");
                return modelAndView;
            }else {
                modelAndView.setViewName("BMS/login");
                System.out.println("账号有冲突，请联系管理员解决");
                return modelAndView;
            }
        }else if (type.equals("种植户")){
            int count = producerService.login(number);
            System.out.println(count);
            if(count == 1) {
                Producer producer = producerService.producer(number, password);
                session.setAttribute("producer",producer);
                modelAndView.setViewName("redirect:/PBmsIndex");
                return modelAndView;
            }else if (count==0){
                modelAndView.setViewName("BMS/login");
                System.out.println("账号密码没有输入");
                return modelAndView;
            }else {
                modelAndView.setViewName("BMS/login");
                System.out.println("账号有冲突，请联系管理员解决");
                return modelAndView;
            }
        }
        return modelAndView;
    }
}
